const QQMapWX = require('../static/js/qqmap-wx-jssdk.min');
const qqKey = 'XCEBZ-MEE3F-XAZJN-NKBX7-HXLTS-BIF6J'
class Location {
  reverseGeocoder () {
    let city = '全国'
    var location = new Promise((resolve, reject) => {
      if (wx.getStorageSync('address')) {
        resolve(wx.getStorageSync('address'))
        return
      }
      wx.getLocation({
        type: 'wgs84',
        success: function (res) {
          console.log(res);
          let qqMapSdk = new QQMapWX({ key: qqKey });
          // 调用接口
          qqMapSdk.reverseGeocoder({
            location: {
              latitude: res.latitude,
              longitude: res.longitude
            },
            success: function (res) {
              city = res.result.ad_info
              resolve(city)
            }
          });
        },
        fail: function (res) {
          console.log(res)
          resolve(city)
        }
      })
    })
    return location
  }

  geocoder (address) {
    console.log(address)
    let city = '全国'
    let location = new Promise(resolve => {
      console.log(address)
      let qqMapSdk = new QQMapWX({ key: qqKey });
      qqMapSdk.geocoder({
        address: address,
        success: function (res) {
          console.log(res);
          resolve(res.result.location)
        },
        fail: function (res) {
          resolve(res)
        },
        complete: function (res) {
          console.log(res);
        }
      })
    })
    return location
  }


}
export { Location }
